package org.liveboardgames.agricola.control.delegates.action.processor
{
    import mx.collections.ListCollectionView;
    
    import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.model.PopupModel;
    import org.liveboardgames.agricola.vo.action.Action;
    import org.liveboardgames.agricola.vo.card.Card;
    import org.liveboardgames.agricola.vo.common.Target;
    import org.liveboardgames.agricola.vo.farm.Farm;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.HashCollection;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class AnimalActionProcessor extends ActionProcessor
    {
        private var quantity:int = 0;
        
        private var type:String;
        
        public function AnimalActionProcessor(action:Action, type:String = null, quantity:int = 0)
        {
            super(action);
            this.type = type;
            this.quantity = quantity;
        }
        
        override public function execute():void
        {
            var farm:Farm = Player(action.target).farm;
            
            var animals:HashCollection;
            
            var link:Target = action.link;
            // First check if the action itself does not specify an animal
            if (type != null && quantity > 0)
            {
                animals = new HashCollection();
                animals.put(type, quantity);
            }
            // The card that holds the animals to be taken
            else if (link != null && link is Card)
            {
                var card:Card = link as Card;
                var resourcesOnCard:IMap = card.resources;
                
                // Initiate the animals to be dispatched
                animals = new HashCollection();
                for each (var animal:String in resourcesOnCard.getKeys())
                {
                    animals.put(animal, resourcesOnCard.getValue(animal));
                }
            }
            
            // Get the cooking places the player can use
            var cookingPlaces:ListCollectionView = GameHelper.getCookingPlaces(farm);
            
            AgricolaModelLocator.getInstance().popups.currentActionPopupIndex = PopupModel.POPUP_ANIMALS_INDEX;
            AgricolaModelLocator.getInstance().popups.animals = animals;
            AgricolaModelLocator.getInstance().popups.cookingPlaces = cookingPlaces;
        }
    }
}